package com.kwan.xframe.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import androidx.core.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.kwan.xframe.BaseApplication;
import com.orhanobut.logger.Logger;

import java.lang.reflect.InvocationTargetException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Random;


public class SystemUtil {

	public static final int NETWORK_NONE = 0;
	public static final int NETWORK_MOBILE = 1;
	public static final int NETWORK_WIFI = 2;

	public static String getAppPackageName() {
		return BaseApplication.getInstance().getPackageName();
	}


	public static boolean checkNetWorkAvailable() {
		//Log.d("BaseModel", "checkNetWorkAvailable:" + SystemUtil.getNetworkState());
		return SystemUtil.getNetworkState() != SystemUtil.NETWORK_NONE;
	}

	public static int getNetworkState() {

		ConnectivityManager connManager = (ConnectivityManager) BaseApplication
				.getInstance().getSystemService(
						Context.CONNECTIVITY_SERVICE);
		// Wifi
		State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();

		if (state == State.CONNECTED || state == State.CONNECTING) {
			return NETWORK_WIFI;
		}

		// 3G
		state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
				.getState();
		if (state == State.CONNECTED || state == State.CONNECTING) {
			return NETWORK_MOBILE;
		}
		return NETWORK_NONE;
	}

	public static String milliSecond2Date(String template, long time) {
		Date d = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat(template, Locale.getDefault());
		return sdf.format(d);
	}

	public static String getUniqueId(Context context) {
		//将使用Android的ID必要作为UniqueID的组成部分
		String device_id = android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
		System.out.println("ANDROID_ID:" + device_id);

		try {
			//如果有电话设备，将加入DeviceId作为UniqueID的组成部分
			TelephonyManager tm = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);

			if (tm != null) {
				if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
					 device_id += tm.getDeviceId();
				}
				System.out.println("DEVICE_ID:" + tm.getDeviceId());
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		//如果有WIFI设备，将加入WIFI设备MAC作为UniqueID的组成部分
		if (null != getMacAddress(context)) {
			String mac = getMacAddress(context);
			System.out.println("MAC:" + mac);
			if (mac != null)
				device_id += mac;
		}

		System.out.println("UNIQUE_ID:" + (device_id = MD5(device_id)));
		return device_id;
	}

	public static String getMacAddress(Context context) {
		String mac = null;
		if (Build.VERSION.SDK_INT < 23) {
			WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
			WifiInfo info = wifi.getConnectionInfo();
			if (info != null)
				mac = info.getMacAddress();
		} else {
			try {
				Class sysConfig;
				sysConfig = Class.forName("android.os.SystemProperties");
				String deviceName = (String) sysConfig.getMethod("get", String.class, String.class).invoke(null, "wifi.interface", "wlan0");
				if (deviceName == null)
					return null;
				if (null == NetworkInterface.getByName(deviceName)) {
					return null;
				}
				if (null == NetworkInterface.getByName(deviceName).getHardwareAddress()) {
					return null;
				}
				byte[] bytesMac = NetworkInterface.getByName(deviceName).getHardwareAddress();
				StringBuilder buf = new StringBuilder();
				for (byte b : bytesMac) {
					buf.append(String.format("%02X:", b));
				}
				if (buf.length() > 0) {
					buf.deleteCharAt(buf.length() - 1);
				}
				mac = buf.toString();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (SocketException e) {
				e.printStackTrace();
			}

		}
		return mac;
	}

	public static String MD5(String s) {
		char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
		try {
			byte[] btInput = s.getBytes();
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(btInput);
			byte[] md = mdInst.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (byte byte0 : md) {
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String md5(String string) {
		if (TextUtils.isEmpty(string)) {
			return "";
		}
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			byte[] bytes = md5.digest(string.getBytes());
			String result = "";
			for (byte b : bytes) {
				String temp = Integer.toHexString(b & 0xff);
				if (temp.length() == 1) {
					temp = "0" + temp;
				}
				result += temp;
			}
			return result;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 获取当前的本地时间戳
	 */
	public static String getLocalTimestamp() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss",Locale.CHINA);
		Random random = new Random();
		int number = random.nextInt(100000000);
		return sDateFormat.format(new java.util.Date()) + number;
	}


	public static String getIPAddress(Context context) {
		NetworkInfo info = ((ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
		if (info != null && info.isConnected()) {
			if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络
				try {
					//Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
					for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
						NetworkInterface intf = en.nextElement();
						for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
							InetAddress inetAddress = enumIpAddr.nextElement();
							if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
								return inetAddress.getHostAddress();
							}
						}
					}
				} catch (SocketException e) {
					e.printStackTrace();
				}

			} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
				WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
				WifiInfo wifiInfo = wifiManager.getConnectionInfo();
				return intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址
			}
		} else {
			Logger.e("当前无网络连接,请在设置中打开网络");
		}
		return null;
	}

	/**
	 * 将得到的int类型的IP转换为String类型
	 *
	 * @param ip
	 * @return
	 */
	public static String intIP2StringIP(int ip) {
		return (ip & 0xFF) + "." +
				((ip >> 8) & 0xFF) + "." +
				((ip >> 16) & 0xFF) + "." +
				(ip >> 24 & 0xFF);
	}


	/**
	 * 获取本地软件版本号
	 */
	public static int getLocalVersion(Context ctx) {
		int localVersion = 0;
		try {
			PackageInfo packageInfo = ctx.getApplicationContext()
					.getPackageManager()
					.getPackageInfo(ctx.getPackageName(), 0);
			localVersion = packageInfo.versionCode;
			Log.d("TAG", "本软件的版本号。。" + localVersion);
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return localVersion;
	}

	/**
	 * 获取本地软件版本号名称
	 */
	public static String getLocalVersionName(Context ctx) {
		String localVersion = "";
		try {
			PackageInfo packageInfo = ctx.getApplicationContext()
					.getPackageManager()
					.getPackageInfo(ctx.getPackageName(), 0);
			localVersion = packageInfo.versionName;
			Log.d("TAG", "本软件的版本号。。" + localVersion);
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return localVersion;
	}
}
